package club.zhilin.guli.common.base.result;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.Date;
import java.util.Map;

/**
 * @author zhilin
 * @since 2022/1/11 16:58
 */
@Data
@ApiModel(value = "统一返回结果对象", description = "包装所有返回结果")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CommonResult<T> {
    @ApiModelProperty(value = "响应信息", example = "Success")
    private String info;
    @ApiModelProperty(value = "响应码", example = "200 (成功),100 (失败)")
    private Integer code;
    @ApiModelProperty(value = "响应对象,传递给前端展示")
    private T data;
    @ApiModelProperty(value = "响应信息", example = "操作成功")
    private String message;
    @ApiModelProperty(value = "响应时间", example = "2022-1-11 17:05:43")
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private Date time;

    private CommonResult(String info, Integer code, T data, String message) {
        this.info = info;
        this.code = code;
        this.data = data;
        this.message = message;
        this.time = new Date();
    }

    public static <T> CommonResult<T> success() {
        return new CommonResult<>(CommonResultCodeEnum.SUCCESS.getMessage(), CommonResultCodeEnum.SUCCESS.getCode(), null, null);
    }

    public static <T> CommonResult<T> success(String message) {
        return new CommonResult<>(CommonResultCodeEnum.SUCCESS.getMessage(), CommonResultCodeEnum.SUCCESS.getCode(), null, message);
    }

    public static <T> CommonResult<T> success(T data) {
        return new CommonResult<>(CommonResultCodeEnum.SUCCESS.getMessage(), CommonResultCodeEnum.SUCCESS.getCode(), data, null);
    }

    public static <T> CommonResult<T> success(String message, T data) {
        return new CommonResult<>(CommonResultCodeEnum.SUCCESS.getMessage(), CommonResultCodeEnum.SUCCESS.getCode(), data, message);
    }

    public static CommonResult<Map<String, Object>> success(Map<String, Object> data) {
        return new CommonResult<>(CommonResultCodeEnum.SUCCESS.getMessage(), CommonResultCodeEnum.SUCCESS.getCode(), data, null);
    }

    public static <T> CommonResult<T> success(CommonResultCodeEnum resultCodeEnum) {
        return new CommonResult<>(resultCodeEnum.getMessage(), resultCodeEnum.getCode(), null, null);
    }

    public static <T> CommonResult<T> failed(CommonResultCodeEnum resultCodeEnum) {
        return new CommonResult<>(resultCodeEnum.getMessage(), resultCodeEnum.getCode(), null, null);
    }

    public static <T> CommonResult<T> failed(CommonResultCodeEnum resultCodeEnum, String message) {
        return new CommonResult<>(resultCodeEnum.getMessage(), resultCodeEnum.getCode(), null, message);
    }

    public static <T> CommonResult<T> failed(String message) {
        return new CommonResult<>("Failed", 100, null, message);
    }

    public static <T> CommonResult<T> failed(String message, Integer code) {
        return new CommonResult<>("Failed", code, null, message);
    }


    @JsonIgnore
    public boolean isSuccess() {
        return this.code.equals(CommonResultCodeEnum.SUCCESS.getCode());
    }
}
